package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.DrivingLicenseBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.DrivingLicenseMapper;
import com.molichuxing.services.infrastructure.dto.request.modify.DrivingLicenseModifyDto;
import com.molichuxing.services.infrastructure.dto.response.DrivingLicenseDto;
import com.molichuxing.services.infrastructure.service.DrivingLicenseService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 
 * @author liyg
 * 驾驶证服务实现类
 * 2019-08-12
 * 
 */
@Service("drivingLicenseService")
public class DrivingLicenseServiceImpl implements DrivingLicenseService{
	
	@Autowired
	private DrivingLicenseMapper drivingLicenseMapper;

	/**
	 * 获取用户驾驶证信息
	 * 
	 * @param userId
	 *            用户id
	 * @return
	 */
	@Override
	public DrivingLicenseDto getDrivingLicense(Integer userId) {
		
		DrivingLicenseBean drivingLicense = drivingLicenseMapper.getById(userId);
		DrivingLicenseDto beanDto = Convert.toDrivingLicenseDto(drivingLicense);
		
		return beanDto;
	}

	/**
	 * 获取驾驶证列表
	 * 
	 * @param map
	 *            参数
	 * key:id  Integer 用户id 非必填
	 * key:name String 驾驶证姓名 非必填
	 * key:status  Integer 验证状态 非必填
	 * key:pageNum  Integer 当前页
	 * key:pageSize  Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<DrivingLicenseDto> getDrivingLicenseList(Map<String,Object> map) {

		//查询总条数
		long total = drivingLicenseMapper.getDrivingLicensePageCount(map);
		if (total <= 0) {
			return null;
		}

		/*分页查询列表*/ 
		Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
		List<DrivingLicenseBean> list = drivingLicenseMapper.getDrivingLicensePage(map,pageNum,pageSize);
		List<DrivingLicenseDto> drivingLicenseDtoList = Convert.toDrivingLicenseDto(list);
		
		Paged<DrivingLicenseDto> paged = new Paged<>(drivingLicenseDtoList,total, pageSize);
				
		return paged;
	}
	
	/**
	 * 获取驾驶证总条数
	 * 
	 * @param status
	 * @return
	 */
	@Override
	public Long getDrivingLicenseCountBystatus(Integer status) {

		return drivingLicenseMapper.getCountByStatus(status);
	}

	/**
	 * 获取驾驶证总条数
	 * 
	 * @param verifyStatus
	 * @return
	 */
	@Override
	public Long getDrivingLicenseCountByVerifyStatus(Integer verifyStatus) {

		return drivingLicenseMapper.getCountByVerifyStatus(verifyStatus);
	}

	/**
	 * 修改驾驶证审核状态
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modifyVerifyStatus(DrivingLicenseModifyDto modifyDto) {
		
		DrivingLicenseBean drivingLicense = Convert.toDrivingLicense(modifyDto);
		
		int num = drivingLicenseMapper.modifyStatusById(drivingLicense);
		
		return num==1 ? true : false;
	}
	
	/**
	 * 修改驾驶证状态
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modifyStatus(DrivingLicenseModifyDto modifyDto) {
		
		DrivingLicenseBean drivingLicense = drivingLicenseMapper.getById(modifyDto.getId());
		if(null == drivingLicense) {
			throw new BizException(101, "不存在该条信息");
		}
		
		DrivingLicenseBean bean = new DrivingLicenseBean();
		BeanUtils.copyProperties(modifyDto, bean);
		
		int num = drivingLicenseMapper.modifyStatus(bean);
		
		return num==1 ? true : false;
	}
	
	/**
	 * 修改驾驶证
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modify(DrivingLicenseModifyDto modifyDto) {
		DrivingLicenseBean drivingLicense = drivingLicenseMapper.getById(modifyDto.getId());
		int num=0;
		DrivingLicenseBean bean = new DrivingLicenseBean();
		BeanUtils.copyProperties(modifyDto, bean);
		if(null == drivingLicense) {
			num = drivingLicenseMapper.create(bean);
			return num == 1;
		}
		num = drivingLicenseMapper.modify(bean);
		return num == 1;
	}

}
